const db = require("../utils/db");

// 医院信息-获取医院信息
exports.hospitalSelect = async (params) => {
    return new Promise(((resolve, reject) => {
        let sql = '';
        if (params.hi_id) {
            sql = `select * from hospitalinfo where hi_id = '${params.hi_id}'`
        }else if (params.hi_name) {
            sql = `select * from hospitalinfo where hi_name like '%${params.hi_name}%'`
        } else {
            sql = `select * from hospitalinfo`
        }
        db.query(sql).then(res => {
            console.log(res, "hospitalSelect sql查询成功结果")
            let result = {};
            result.list = res.slice((params.pageIndex - 1) * (params.pageSize), (params.pageIndex - 1) * (params.pageSize) + params.pageSize);
            result.total = res.length;
            result.pageIndex = params.pageIndex;
            result.pageSize = params.pageSize;
            resolve(result);
        }).catch(error => {
            console.log(error, "hospitalSelect sql查询失败结果")
            reject(error)

        })
    }))
}
// 医院信息-医院信息添加
exports.hospitalAdd = async (params) => {
    return new Promise(((resolve, reject) => {
        var value = [], key = [], sql = '';
        for (var item in params) {
            key.push(item);
            value.push(`'${params[item]}'`);
        }
        console.log(key.join(','), value.join(','));
        sql = `insert into hospitalinfo (${key.join(',')}) values(${value.join(',')}) `;
        db.query(sql).then(res => {
            console.log(res, "hospitalAdd sql添加成功结果")
            let result = { msg: '添加成功!' };
            resolve(result);
        }).catch(error => {
            console.log(error, "hospitalAdd sql添加失败结果")
            reject(error)

        })
    }))
}
// 医院信息-医院信息修改
exports.hospitalUpdate = async (params) => {
    return new Promise(((resolve, reject) => {
        var str = [], sql = '';
        for (var item in params) {
            str.push(`${item}='${params[item]}'`);
        }
        console.log(str.join(','));
        sql += `update hospitalinfo set ` + str.join(',') + ` where hi_id = '${params.hi_id}'`;
        db.query(sql).then(res => {
            console.log(res, "hospitalUpdate sql修改成功结果");
            let result = {};
            result.msg = '修改成功!'
            resolve(result);
        }).catch(error => {
            console.log(error, "hospitalUpdate sql查询失败结果");
            reject(error)

        })
    }))
}
// 医院信息-医院信息删除
exports.hospitalDelete = async (params) => {
    return new Promise(((resolve, reject) => {
        let sql = `delete from hospitalinfo where hi_id = '${params.hi_id}'`;
        db.query(sql).then(res => {
            console.log(res, "hospitalDelete sql删除成功结果")
            let result = { msg: '删除成功!' };
            resolve(result);
        }).catch(error => {
            console.log(error, "hospitalDelete sql删除失败结果")
            reject(error);

        })
    }))
}